﻿using MQTTnet.Client;
using MQTTnet;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using JESAI.MQTT.Client.Clients;
using System.Collections.Concurrent;
using JESAI.MQTT.Client.Configurations;
using MQTTnet.Server;
using JESAI.MQTT.Client.Enums;

namespace JESAI.MQTT.Client.Publishs
{
    public class MQTTMessagePublish
    {
      
        private static MQTTClientConfig mQTTClientConfig;
      

        public static async Task Publish_Message(MqttMessage mqttMessage)
        {
            MQTTClient mqttClient = MQTTClient.Instance;
            /*
            *此示例推送一个简单的应用程序消息，其中包括一个主题和一个有效负载。
            *
            *总是在存在建造者的地方使用建造者。（本项目中的）建设者
            *向后兼容。通过其构造函数创建_MqttApplicationMessage_也是
            *支持，但在构建器不支持的将来版本中，类可能会经常更改
            *或者至少在可能的情况下提供向后兼容性。
             */
            var applicationMessage = new MqttApplicationMessageBuilder()
                .WithTopic(mqttMessage.Topic)
                .WithPayload(mqttMessage.Message)
                .Build();

            await mqttClient.mqttClient.PublishAsync(applicationMessage, CancellationToken.None);
        }

        public static async Task Publish_Multiple_Messages( List<MqttMessage> messages)
        {
            /*
            *此示例推送多个简单的应用程序消息，其中包括一个主题和一个有效负载。
            *
            *有关更多详细信息，请参见示例_Publish_Application_Message_。
             */
        
            foreach (MqttMessage message in messages)
            {
                MQTTClient mqttClient = MQTTClient.Instance;
                var applicationMessage = new MqttApplicationMessageBuilder()
                    .WithTopic(message.Topic)
                    .WithPayload(message.Message)
                    .Build();

                await mqttClient.mqttClient.PublishAsync(applicationMessage, CancellationToken.None);
            }                       
        }
    }
}
